package com.wangbo.restTemplate.camviExtension;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.wangbo.restTemplate.aiSchool.pojo.ResultObj;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StopWatch;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.Arrays;
import java.util.Base64;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class TestCamviExtension {
    public static final String BASE_URL = "http://192.168.88.32:8080/camvi-extension";
    public static final String MANUAL_SNAP = "/biz/manualSnap";
    public static final AtomicInteger counter = new AtomicInteger(1000);

    public static void main(String[] args) throws InterruptedException {
        RestTemplate restTemplate = new RestTemplateBuilder().errorHandler(new DefaultResponseErrorHandler(){
            /**
             * 设置RestTemplate自带的异常处理器不做异常处理
             **/
            @Override
            public void handleError(ClientHttpResponse response) {
            }
        }).build();

        // 批量手动抓拍
//        StopWatch stopWatch = new StopWatch();
//        stopWatch.start("5张");
//        test01(restTemplate, new File("D:\\test"), 1);
//        stopWatch.stop();
//        stopWatch.start("10张");
//        test01(restTemplate, new File("D:\\test"), 2);
//        stopWatch.stop();
//        stopWatch.start("20张");
//        test01(restTemplate, new File("D:\\test"), 4);
//        stopWatch.stop();
//        stopWatch.start("30张");
//        test01(restTemplate, new File("D:\\test"), 6);
//        stopWatch.stop();
//        stopWatch.start("40张");
//        test01(restTemplate, new File("D:\\test"), 8);
//        stopWatch.stop();
//        stopWatch.start("50张");
//        test01(restTemplate, new File("D:\\test"), 10);
//        stopWatch.stop();
//        System.out.println(stopWatch.prettyPrint());

        // 多线程手动抓拍
        StopWatch stopWatch = new StopWatch();
//        stopWatch.start("1线程100张");
//        test02(restTemplate, new File("D:\\test"), 20, 1);
//        stopWatch.stop();
        stopWatch.start("5线程1000张");
        test02(restTemplate, new File("D:\\test"), 200, 5);
        stopWatch.stop();
//        stopWatch.start("10线程10000张");
//        test02(restTemplate, new File("D:\\test"), 2000, 10);
//        stopWatch.stop();
//        stopWatch.start("20线程100张");
//        test02(restTemplate, new File("D:\\test"), 20, 20);
//        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }

    /**
     * 批量手动抓拍
     */
    public static void test01(RestTemplate restTemplate, File dir, Integer time) {
        for (int i = 0; i < time; i++) {
            for (File file : dir.listFiles()) {
                byte[] bytes = FileUtil.readBytes(file);
                String base64 = Base64.getEncoder().encodeToString(bytes);

                JSONObject requestBody = new JSONObject();
                requestBody.put("cid", 1);
                requestBody.put("picId", counter.incrementAndGet());
                requestBody.put("smallImageBase64", base64);

                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                HttpEntity<JSONObject> httpEntity = new HttpEntity<>(requestBody, headers);
                ResultObj resultObj = restTemplate.postForObject(BASE_URL + MANUAL_SNAP, httpEntity, ResultObj.class);
                System.out.printf("响应结果：%s%n", resultObj.toString());
            }
        }


    }

    /**
     * 多线程手动抓拍
     */
    public static void test02(RestTemplate restTemplate, File dir, int time, int threadNum) throws InterruptedException {
        File[] files = dir.listFiles();
        assert files != null;
        CountDownLatch countDownLatch = new CountDownLatch(files.length * time);

        ConcurrentLinkedQueue<File> taskQueue = new ConcurrentLinkedQueue<>();
        for (int i = 0; i < time; i++) {
            taskQueue.addAll(Arrays.asList(files));
        }

        for (int i = 0; i < threadNum; i++) {
            Thread thread = new Thread(() -> {
                while (!taskQueue.isEmpty()) {
                    File file = taskQueue.poll();

                    byte[] bytes = FileUtil.readBytes(file);
                    String base64 = Base64.getEncoder().encodeToString(bytes);

                    JSONObject requestBody = new JSONObject();
                    requestBody.put("cid", 1);
                    requestBody.put("picId", counter.incrementAndGet());
                    requestBody.put("smallImageBase64", base64);

                    HttpHeaders headers = new HttpHeaders();
                    headers.setContentType(MediaType.APPLICATION_JSON);
                    HttpEntity<JSONObject> httpEntity = new HttpEntity<>(requestBody, headers);
                    ResultObj resultObj = restTemplate.postForObject(BASE_URL + MANUAL_SNAP, httpEntity, ResultObj.class);
                    countDownLatch.countDown();
                }
            }, String.format("thread_%d", i));
            thread.start();
        }
        countDownLatch.await();

    }
}
